package org.geometerplus.android.fbreader;

import org.geometerplus.fbreader.fbreader.ActionCode;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.zlibrary.ui.android.R;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

final class TextSearchPopupTop extends PopupPanel {
	final static String ID = "TextSearchPopupTop";
	ImageButton cancelBtn = null;
	EditText searchText = null;
	ImageButton searchBack = null;
	ImageButton searchForward = null;

	TextSearchPopupTop(FBReaderApp fbReader) {
		super(fbReader);
	}

	public void runSearch() {
		Application.showPopup(ID);
	}

	@Override
	public String getId() {
		return ID;
	}

	@Override
	protected void show_() {
		super.show_();
		if (myWindow != null) {
		}
	}

	@Override
	public void createControlPanel(FBReader activity, RelativeLayout root) {
		if (myWindow != null && activity == myWindow.getActivity()) {
			return;
		}
		myWindow = new PopupWindow(activity, root, PopupWindow.Location.Top,
				true);

		final View layout = activity.getLayoutInflater().inflate(
				R.layout.text_search_popup_top, myWindow, false);

		cancelBtn = (ImageButton) layout.findViewById(R.id.cancel_epub);
		searchText = (EditText) layout.findViewById(R.id.searchText_epub);
		searchBack = (ImageButton) layout.findViewById(R.id.searchBack_epub);
		searchForward = (ImageButton) layout
				.findViewById(R.id.searchForward_epub);
		searchText.requestFocus();
		searchText.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
				if (s.toString().length() > 0) {
					final FBReaderApp fbReader = (FBReaderApp) FBReaderApp
							.Instance();
					fbReader.TextSearchPatternOption.setValue(searchText
							.getText().toString());
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}
		});
		activity.addImageButton(cancelBtn, ActionCode.SHOW_CANCEL);
		activity.addImageButton(searchBack, ActionCode.FIND_PREVIOUS);
		activity.addImageButton(searchForward, ActionCode.FIND_NEXT);
		myWindow.addView(layout);

	}

	@Override
	protected void update() {
		// TODO Auto-generated method stub

	}
}
